package cn.jinjimi.app.main.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

import cn.jinjimi.app.R;

/**
 * Created by Roy
 * Date: 16/4/14
 */
public class RedNumTextView extends TextView {
    int unreadCount = 0;

    public RedNumTextView(Context context) {
        this(context, null);
    }

    public RedNumTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RedNumTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        setTextColor(Color.WHITE);
        TextPaint tp = getPaint();
        tp.setFakeBoldText(true);
        setGravity(Gravity.CENTER);

        setTextSize(TypedValue.COMPLEX_UNIT_PX, getContext().getResources().getDimensionPixelSize(R.dimen.font_11));
    }

    public void setUnreadNum(int count) {
        if (this.unreadCount == count)
            return;

        this.unreadCount = count;

        setVisibility(count <= 0 ? View.GONE : View.VISIBLE);

        if (count > 0 && count < 10) {
            setText(String.valueOf(count));
            setBackgroundResource(R.drawable.unread_red_oval);
        }

        if (count >= 10 && count < 100) {
            setText(String.valueOf(count));
            setBackgroundResource(R.drawable.unread_red_two);
        }

        if (count >= 100) {
            setText("99+");
            setBackgroundResource(R.drawable.unread_red_three);
        }
    }

    public int getUnreadNum() {
        return unreadCount;
    }

}
